package se.webbzon.boltzmann.input;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import net.java.games.input.Component;

import se.webbzon.boltzmann.debug.Debug;
import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.oschi01.resourceset.ResourceSet;
import se.webbzon.oschi01.resourceset.ResourceSetLoaderException;
import se.webbzon.oschi01.textureset.Texture3D;
import se.webbzon.oschi01.textureset.Texture3DSetLoader;

public class GamepadButtonInfo implements ButtonInfo {
	
	// A gamepad stick direction
	public enum Direction {Up, Down, Left, Right};
	
	// The font used for this keyboard button info texture
	private static final Font font = new Font("Lucida Console",Font.BOLD,16);
	
	// The background of this keyboard button info texture
	private static final ResourceSet<Texture3D> background = Resources.getTextureSet("gamepad_button");
	
	// The name of this key
	private final String name;
	
	// The texture set associated with this key
	private final ResourceSet<Texture3D> textureSet;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public GamepadButtonInfo(Component.Identifier.Button button) {
		name = button.getName();
		
		if (button == Component.Identifier.Button.LEFT_THUMB)
			textureSet = Resources.getTextureSet("gamepad_l1");
		else if (button == Component.Identifier.Button.LEFT_THUMB2)
			textureSet = Resources.getTextureSet("gamepad_l2");
		else if (button == Component.Identifier.Button.RIGHT_THUMB)
			textureSet = Resources.getTextureSet("gamepad_r1");
		else if (button == Component.Identifier.Button.RIGHT_THUMB2)
			textureSet = Resources.getTextureSet("gamepad_r2");
		else if (button == Component.Identifier.Button.START)
			textureSet = Resources.getTextureSet("gamepad_start");
		else {
			// Create image
			BufferedImage image = new BufferedImage(32,32,BufferedImage.TYPE_INT_ARGB);
			Graphics2D g2d = image.createGraphics();
			g2d.drawImage(background.get(0).getImage(), 0, 0, null);
			g2d.setFont(font);
			g2d.setColor(Color.black);
			g2d.drawString(name, 12, 22);
			g2d.setColor(Color.white);
			g2d.drawString(name, 11, 21);

			// Create texture
			Texture3DSetLoader loader = new Texture3DSetLoader();
			try {
				loader.addFromBufferedImage(image);
			} catch (ResourceSetLoaderException e) {
				Debug.report(e);
			}
			textureSet = loader.getResourceSet();
		}
	}
	
	public GamepadButtonInfo(Direction dir) {
		name = dir.name();
		switch (dir) {
		case Up: textureSet = Resources.getTextureSet("axis_up"); break;
		case Down: textureSet = Resources.getTextureSet("axis_down"); break;
		case Left: textureSet = Resources.getTextureSet("axis_left"); break;
		case Right: textureSet = Resources.getTextureSet("axis_right"); break;
		default: textureSet = null;
		}
	}
	
	/*============================================================
	Public Methods
	============================================================*/

	@Override public String getName() {
		return name;
	}

	@Override public ResourceSet<Texture3D> getTextureSet() {
		return textureSet;
	}

}
